package framework 
{
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.DataLoader;
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.SWFLoader;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.system.ApplicationDomain;
	import framework.data.PreLoadList;
	import framework.layer.LayerManager;
	import framework.util.fps.C5FPS;
	import framework.util.log.Logger;
	import Global.GConfig;
	/**
	 * 管理框架
	 * @author Henrylee{henrylee_sz@qq.com}
	 */
	public final class FrameworkAdmin 
	{
		
		static private var _onDoneInitFrameWork: Function;
		
		static public var STAGE: Stage;
		
		static public var loadMax: LoaderMax;
		
		static private var _instance: FrameworkAdmin;
		
		public function FrameworkAdmin()
		{
			
		}
		
		static public function get instance(): FrameworkAdmin
		{
			if (_instance == null)
			{
				_instance = new FrameworkAdmin();
			}
			return _instance;
		}
		
		static public function init(stage: Stage, onDone: Function):void
		{
			STAGE = stage;
			_onDoneInitFrameWork = onDone;
			
			Logger.logger.init(STAGE);
			if (GConfig.bDebug)
			{
				new C5FPS().startCalc(STAGE);
			}
			
			FrameworkAdmin.instance.preLoadData();
		}
		
		
		private function preLoadData(): void
		{
			loadMax = new LoaderMax( { name:"preLoadQueue", onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler } );
			
			var len: int = PreLoadList.PRE_LOAD.length;
			var i: int = 0;
			while (i < len)
			{
				loadMax.append( new DataLoader(PreLoadList.PRE_LOAD[i], { name:"preLoad" + i } ) );
				i++;
			}
			
			len = PreLoadList.PRE_LOAD_ASSET.length;
			i = 0;
			while (i < len)
			{
				loadMax.append( new SWFLoader(PreLoadList.PRE_LOAD_ASSET[i], { name:"swfload" + i, autoPlay:false} ) );
				i++;
			}
			
			loadMax.load();
		}
		
		private function initLayer():void
		{
			LayerManager.instance.init(STAGE, PreLoadList.PRE_DATA_MAP[0]);
			_onDoneInitFrameWork();
		}
		
		private function progressHandler(event:LoaderEvent):void
		{
			Logger.log("progress: " + event.target.progress);
		}
		
		private function completeHandler(event:LoaderEvent):void
		{
			Logger.log(event.target + " is complete!");
			
			var len: int = PreLoadList.PRE_LOAD.length;
			var i: int = 0;
			while (i < len)
			{
				PreLoadList.PRE_DATA_MAP.push(LoaderMax.getContent("preLoad" + i));
				i++;
			}
			
			initLayer();
		}
		
		private function errorHandler(event:LoaderEvent):void
		{
			Logger.log("error occured with " + event.target + ": " + event.text);
		}
		
		/**
		 * 获得一个类定义
		 * @param	className
		 * @return
		 */
		public function getClass(className:String):Class
		{
			var swfLoader: SWFLoader = loadMax.getLoader(PreLoadList.PRE_LOAD_ASSET[0]);
			if (swfLoader != null)
			{
				return swfLoader.getClass("framework."+className);
			}
			return null;
		}
		
		/**
		 * 获得一个类定义的实例对象
		 * @param	className
		 * @return
		 */
		public function getMovieClip(className:String): MovieClip
		{
			var CLASS: Class = getClass(className);
			if (CLASS != null) {
				return MovieClip(new CLASS());
			}
			return null;
		}
		
	}

}